package com.bzyd.shiro.common.exception;

import com.bzyd.shiro.common.SysResult;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public SysResult doHandleRuntimeException(RuntimeException e) {
        e.printStackTrace();
        if (e instanceof ServiceException) {
            return SysResult.fail(e);
        }

        if (e instanceof ShiroException) {
            if (e instanceof UnknownAccountException)
                return SysResult.fail("账户不存在！");
            if (e instanceof LockedAccountException)
                return SysResult.fail("账户已被禁用！");
            if (e instanceof IncorrectCredentialsException)
                return SysResult.fail("密码不正确！");
            if (e instanceof AuthorizationException)
                return SysResult.fail("没有权限！");
        }

        return SysResult.fail("服务器异常！");
    }
}
